/*kvan
 *2014.5.12
 */
package com.taxsystem.likaifan.taxcompute;

import java.util.concurrent.ExecutionException;

import org.json.JSONException;
import org.json.JSONObject;

import com.main.taxsystem.R;
import com.taxsystem.tool.HttpUtil;

import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi") public class VehicleTaxCompute extends Activity {

	final int MY_NOTICE = 0x111;
	final int NAV_SETTING = 0x112;
	final int ACCOUNT_SETTING = 0x113;
	final int INDIVIDUAL_SETTING = 0x114;
	public final static String RECI_COAST = "sw.system.HTTUTIL";
	private TextView show_result;
	
    private static final String[] type = {"小轿车","卡车","客车"};
    private static final String[] displacement = {"1L","2L","3L"};

    
	private Spinner vehicle_type;
	private Spinner vehicle_displacement;

	private String vehicle_type_str;
	private String vehicle_displacement_str;
	
	private Button button;
	
	private String requestUrl;
	
	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vehicle_tax_compute);
		ActionBar bar = getActionBar();
        bar.setHomeButtonEnabled(true);
        bar.setIcon(R.drawable.actionbar_icon);
        bar.setDisplayHomeAsUpEnabled(true);
        //bar.setDisplayShowHomeEnabled(false);
        bar.setBackgroundDrawable(new ColorDrawable(0xff333333));       
		show_result = (TextView) findViewById(R.id.show_result);
		
		vehicle_type = (Spinner) findViewById(R.id.vehicle_type);
		vehicle_displacement = (Spinner) findViewById(R.id.vehicle_displacement);
		
		vehicle_type.setPrompt("请选择收入类型"); 
		ArrayAdapter<String> type_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, type);
	    type_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
	    vehicle_type.setAdapter(type_adapter);  

	    vehicle_type.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

	           public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
	           {

	       		//Toast msg = Toast.makeText(individualIncomeTaxCompute.this, "选择的是"+m_arr[arg2], Toast.LENGTH_LONG);
	       		//msg.show();
	        	   vehicle_type_str = type[arg2];
	        	   arg0.setVisibility(View.VISIBLE);

	           }

	           public void onNothingSelected(AdapterView<?> arg0)
	           {

	               //

	           }

	       });


	    
	    vehicle_displacement.setPrompt("请选择收入类型"); 
		ArrayAdapter<String> displacement_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, displacement);
		displacement_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
	    vehicle_displacement.setAdapter(displacement_adapter);  

	    vehicle_displacement.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

	           public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
	           {

	       		//Toast msg = Toast.makeText(individualIncomeTaxCompute.this, "选择的是"+m_arr[arg2], Toast.LENGTH_LONG);
	       		//msg.show();
	        	   vehicle_displacement_str = displacement[arg2];
	        	   arg0.setVisibility(View.VISIBLE);

	           }

	           public void onNothingSelected(AdapterView<?> arg0)
	           {

	               //

	           }

	       });
	    

		// 注册广播
		IntentFilter filter = new IntentFilter(RECI_COAST);
		BroadcastReceiver myrecive = new MyRecive();
		registerReceiver(myrecive, filter);
		Thread thread = new Thread() 
		{

			@Override
			public void run() 
			{
				Intent intent = new Intent(RECI_COAST);
				try {
					//获取服务器返回的信息
					String reslut = HttpUtil
							.getRequest(requestUrl);
					intent.putExtra("weatherinfo", reslut);
					//发送广播
					sendBroadcast(intent);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}
			}

		};
	}

	
	public void check(View view)
	{
		 requestUrl = "收入类型为："+vehicle_type_str+"\n收入数额为："+vehicle_displacement_str+ "\n查询结果:未拿到接口";
		// Toast msg = Toast.makeText(ActivityMian.this, "Message", Toast.LENGTH_LONG);
		// msg.show();
		 show_result.setText(requestUrl);
	}
	
	// 定义BroadcastReceiver 接收返回的信息
	private class MyRecive extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				//获取JSONObject对象
				JSONObject jsonobject = new JSONObject(
						intent.getStringExtra("weatherinfo"));
				JSONObject jsoncity = new JSONObject(
						jsonobject.getString("weatherinfo"));
				show_result.setText("城市:" + jsoncity.getString("city") + "\t"
						+ "日期:" + jsoncity.getString("date_y") + "\n" + "今天天气:"
						+ jsoncity.getString("temp1") + "\t"
						+ jsoncity.getString("weather1")+"\t"+jsoncity.getString("wind1"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_mian, menu);
		menu.add(0, MY_NOTICE, 0, "我的通知");
		menu.add(0, NAV_SETTING, 0, "导航设置");
		menu.add(0, ACCOUNT_SETTING, 0, "账号设置");
		menu.add(0, INDIVIDUAL_SETTING, 0, "个性化设置");
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem mi)
	{
		
		
		//判断单击的是哪个菜单项，并针对性的作出响应。
		switch (mi.getItemId())
		{
			case android.R.id.home:
				this.finish();
				break;
			case MY_NOTICE:
				Log.d("hehe","heh");
				Toast toast1 = Toast.makeText(VehicleTaxCompute.this,
					     "MY_NOTICE", Toast.LENGTH_SHORT);
				toast1.show();
				break;
			case NAV_SETTING:
				Toast toast2 = Toast.makeText(getApplicationContext(),
					     "NAV_SETTING", Toast.LENGTH_SHORT);
				toast2.show();;
				break;
			case ACCOUNT_SETTING:
				Toast toast3 = Toast.makeText(getApplicationContext(),
					     "ACCOUNT_SETTING", Toast.LENGTH_SHORT);
				toast3.show();
				break;
			case INDIVIDUAL_SETTING:
				Toast toast4 = Toast.makeText(getApplicationContext(),
					     "INDIVIDUAL_SETTING", Toast.LENGTH_SHORT);
				toast4.show();
				break;

		}
		return true;
	}

	
}
